<template>
   <ex-dialog v-model="visible" draggable :title="form.name ? form.name : (form.id ? form.name : '新增')"
	width="700" :contentHeight="450">

    <template #default>
      <el-tabs v-model="activeName"  style="height: 100%">
        <el-tab-pane label="基本信息" name="base" style="height: 100%">
          <el-row :gutter="80">
            <el-col :lg="24">
              <el-form :model="form" :rules="button_rules" ref="dialogForm" label-width="80px" label-position="left">
                <el-form-item label="编码" prop="code">
                  <el-input v-model="form.code" clearable placeholder="编码"></el-input>
                </el-form-item>
                <el-form-item label="名称" prop="name">
                  <el-input v-model="form.name" clearable placeholder="名称"></el-input>
                </el-form-item>
                <el-form-item label="排序" prop="orderWeight">
					<el-input v-model="form.orderWeight" clearable placeholder="排序"></el-input>
				  </el-form-item>
                <el-form-item label="金额" prop="amount">
					<el-input v-model="form.amount" clearable placeholder="金额"></el-input>
				  </el-form-item>
          <el-form-item label="开始日期" prop="startDate">
          <el-date-picker
                            v-model="form.startDate"
                            type="date"
							value-format="YYYYMMDD"
							format="YYYY-MM-DD"
                            placeholder="开始日期"
            >
            </el-date-picker>
				  </el-form-item>
          <el-form-item label="结束日期" prop="endDate">
            <el-date-picker
                            v-model="form.endDate"
                            type="date"
							value-format="YYYYMMDD"
							format="YYYY-MM-DD"
                            placeholder="结束日期"
            >
            </el-date-picker>
				  </el-form-item>
          <el-form-item label="备注" prop="description">
                  <el-input v-model="form.description" clearable placeholder="备注"></el-input>
                </el-form-item>
              </el-form>

            </el-col>
          </el-row>
        </el-tab-pane>
        <el-tab-pane label="资产类别" name="Sysassetcategory" style="height: 100%">
			<ex-rel-table ref="assetcategoryRelTable" size="small" :rel-entity="form" :treeApi="this.$API.asset.assetcategory.treeData" default-expand-all>
			</ex-rel-table>
		  </el-tab-pane>
      <el-tab-pane label="预算部门" name="Sysdept" style="height: 100%">
			<ex-rel-table ref="deptRelTable" size="small" :rel-entity="form" :treeApi="this.$API.system.dept.treeData" default-expand-all>
			</ex-rel-table>
		  </el-tab-pane>
      </el-tabs>
    </template>
    <template #footer>
      <div style="flex: auto;text-align: center">
        <el-button plain type="primary" @click="save" :loading="loading">保 存</el-button>
        <el-button plain type="warning" @click="reset" :loading="loading">重 置</el-button>
        <el-button plain type="info" @click="this.visible = false" :loading="loading">关 闭</el-button>
      </div>
    </template>

  </ex-dialog>
</template>

<script>
import scIconSelect from '@/components/scIconSelect'
import ExRelTable from "@/components/exRelTable/index.vue";
import exDialog from '@/components/exDialog';
import BaseTemplate from "@/utils/baseTemplate";

export default {
  computed: {
  },
  emits: ['success'],
  components: {
    ExRelTable,
    scIconSelect,
    exDialog
  },
  props: {
    budget: {
      type: Object, default: () => {
      }
    },
  },
  data() {
    return {
      activeName: 'base',
      visible: false,
      form: {
        id: "",
        name: "",
        parentLevelCode:"",
        mainCategoryCode:"",
        externalCode:"",
        description: "",
        type:"",
        remindDay:"",
      },


      button_rules: {
        name: [
          {required: true, message: "请输入预算名称", trigger: "blur"},
        ],
		  startDate:[
			  {required: true, message: "选择预算开始日期", trigger: "blur"},
		  ],
		  endDate:[
			  {required: true, message: "选择预算结束日期", trigger: "blur"},
		  ]
      },
      loading: false,
      oldForm: null,
    }
  },
  watch: {},
  mounted() {

  },
	methods: {
    async initData() {
    },
    relHandler(relMap) {
		if (!this.form.relMaps) {
		  this.form.relMaps = {};
		}
		for (let key in relMap) {//清空旧的数据
		  this.form.relMaps[key] = {};
		}
		//重新赋值新数据
		Object.assign(this.form.relMaps, relMap);
	  },
    //显示
    open(entity) {
      this.form = Object.assign({}, entity);
      this.oldForm = Object.assign({}, this.form);
      this.visible = true;
      this.activeName = "base";
	  this.initData();
    },
    reset() {
      this.form = Object.assign({}, this.oldForm);
		for(let ref in this.$refs){
			try {
				this.$refs[ref].refresh();
			}catch (e){
			}
		}
    },
    //保存
    async save() {
      let valid = await BaseTemplate.validate(this,this.$refs.dialogForm);
      if (valid) {
        try {
          this.loading = true
			if(this.form.relMaps.FamsAssetCategory.length === 0){
				this.$message.warning("请勾选预算的资产分类");
				return;
			}
			if(this.form.relMaps.SysOrganization.length === 0){
				this.$message.warning("请勾选预算部门");
				return;
			}
			if(this.form.endDate.substr(0,4) != this.form.startDate.substr(0,4)){
				this.$message.warning("预算的开始日期和结束日期必须为同一年份");
				return;
			}
          var res;
          if (this.form.id) {
            res = await this.$API.system.budget.edit.post(this.form)
          } else {
			 this.form.year = this.form.endDate.substr(0,4);
			  res = await this.$API.system.budget.add.post(this.form)
          }
          if (res.code === 2000) {

            this.$message.success(this.$t("code."+res.code));
			this.$emit('success');
            this.visible = false;
			this.$store.dispatch("commit",this.form.entityName);
          } else {
            this.$message.warning(res.message)
          }
        } finally {
          this.loading = false
        }
      }
    },
  }
}
</script>

<style scoped>
h2 {
  font-size: 17px;
  color: #3c4a54;
  padding: 0 0 30px 0;
}

[data-theme="dark"] h2 {
  color: #fff;
}

</style>
